function cmsGrid()
{
    $('#cmsGrid').jqGrid({
                url: siteUrl + 'cms/get_cms',
                mtype: 'post',
                datatype: 'json',
                colNames: ['ID', 'Information Type', 'Operations'],
                colModel: [
                {name: 'information_id', index: 'information_id', hidden: true, key: true},
                {name: 'information_type', index: 'information_type'},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: cmsOperations}
                ],


            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'information_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true,
            ondblClickRow: function(rowId, iRow, iCol, e) {
                window.location = siteUrl + 'cms/view/' + rowId;
            }
    });
}
function cmsOperations(cell, opts, rowObj)
{
    var id = opts.rowId;
    
    var edit = '<a href="' + siteUrl + 'cms/edit/' + id + '" title="Update" class="icon icon-update"></a>';

    var del = '<a href="javascript:$.confirmDelete(' + id + ', \'deleteContent\', \'Content - Delete Record\')" title="Delete" class="icon icon-delete"></a>';

    
    return edit;
}

function cmsRules()
{
    
    	$.validator.addMethod('ckEditor', function() {
				var editorcontent = CKEDITOR.instances['content'].getData().replace(/<[^>]*>/gi, '').replace(/(^\s*)/g, '');
				if (editorcontent.length){
				  return true;
				}
				else{
				  return false;
				}
			}, $.Msgs('required', 'content'));


    $('#cms_form').validate({
        debug: true,
        rules: {
            content: {
                ckEditor: true
            }

        },
        messages: {

        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
//            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            form.submit();
        }
    });

}

function saveContent(){
    $.loading('Content - Adding Content');
    
    $.ajax({
        url: siteUrl + 'cms/submit',
        type: 'post',
        data: $('#cms_form').serialize(),
        success: function(response) {
            console.debug(response);
            if (response)
            {;
                window.location = siteUrl + 'cms';
            }
        }
    });
}

function deleteContent(contentId)
{
    $.loading('Content - Deleting Record');

    $.ajax({
        url: siteUrl + 'cms/delete/' + contentId,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'cms';
            }

        }
    });
}